package ss.pku.jsa.type.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class AnnotationReader {
	BufferedReader in;

	public AnnotationReader(String filename) throws IOException {
		this(new BufferedReader(new FileReader(filename)));
	}

	public AnnotationReader(BufferedReader in) {
		this.in = in;
	}

	public AnnotationStore readAll() throws IOException {
		AnnotationStore ret = new AnnotationStore();
		QSig sig = null;
		while ((sig = next()) != null) {
			ret.put(sig);
		}
		return ret;
	}

	public QSig next() throws IOException {
		String tag = this.in.readLine();

		while ((tag != null) && (tag.length() == 0)) {
			tag = this.in.readLine();
		}
		if (tag == null) {
			return null;
		}
		if (tag.equals("method:"))
			return MethodQSig.read(this.in);
		if (tag.equals("field:")) {
			return FieldQSig.read(this.in);
		}
		throw new IllegalArgumentException("unknown tag: " + tag);
	}
}
